package com.star.carservice.client.fragment;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.star.carservice.R;
import com.star.carservice.client.activity.RegisterActivity;
import com.star.carservice.client.fragment.base.BaseFragment;
import com.star.carservice.client.utils.PreferencesUtils;
import com.star.carservice.client.utils.StringUtils;
import com.star.carservice.client.utils.ToastUtils;
import com.star.carservice.client.widget.MyProgressDialog;
import com.star.carserviceapi.domain.request.CreateAccountRequest;
import com.star.carserviceapi.domain.result.CreateAccountResult;
import com.star.carserviceapi.domain.result.VerifyValidCodeResult;

public class RegisterStep3Fragment extends BaseFragment {

	private final static String TAG = RegisterStep3Fragment.class
			.getSimpleName();

	MyProgressDialog mProgressDialog = null;

	@InjectView(R.id.input_pwd1)
	EditText input_pwd1;

	@InjectView(R.id.input_pwd2)
	EditText input_pwd2;

	@Override
	public String getLogTag() {
		return TAG;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		mProgressDialog = new MyProgressDialog(getActivity(), false);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		if (CacheViewIsNull()) {
			mCacheView = inflater.inflate(R.layout.frag_register_step3,
					container, false);

			ButterKnife.inject(this, mCacheView);
		}

		removeCacheView();

		return mCacheView;
	}

	@OnClick(R.id.btn_submit_code)
	public void onCreateAccount() {
		RegisterActivity parent = (RegisterActivity) getActivity();

		String mobile = parent.getMobile();

		String pwd1 = input_pwd1.getText().toString().trim();
		String pwd2 = input_pwd2.getText().toString().trim();

		if (StringUtils.isBlank(mobile)) {
			return;
		}

		execAsyncTask(new CreateAccountTask(), new CreateAccountRequest(mobile,
				pwd1, pwd2));
	}

	class CreateAccountTask extends
			AsyncTask<CreateAccountRequest, Void, CreateAccountResult> {

		@Override
		protected void onPreExecute() {
			mProgressDialog.show();
		}

		@Override
		protected CreateAccountResult doInBackground(
				CreateAccountRequest... params) {

			return getApi().createAccount(params[0]);
		}

		@Override
		protected void onPostExecute(CreateAccountResult result) {
			mProgressDialog.dismiss();

			if (result.successful()) {
				RegisterActivity parent = (RegisterActivity) getActivity();

				PreferencesUtils.setUserId(result.getData().getUid());
				PreferencesUtils.setMobile(parent.getMobile());
				PreferencesUtils.setAccessToken(result.getData()
						.getAccesstoken());

				getActivity().finishActivity(Activity.RESULT_OK);
			} else {
				ToastUtils.show(getActivity(), result.getMessage());
			}
		}

	}
}
